<?php

declare(strict_types=1);

namespace app\group\model\order;

use sheep\model\Common;
use sheep\model\auth\Admin;

class Action extends Common
{
    protected $name = 'shop_order_action';
    protected $connection = 'mysql_cutepet';
    protected $type = [
    ];

    // 追加属性
    protected $append = [
        'oper'
    ];


    public static function add($order, $item = null, $oper = null, $type = 'user', $remark = '')
    {
        $oper_id = $oper ? $oper->id : 0;
        $self = new self();
        $self->order_id = $order->id;
        $self->order_item_id = is_null($item) ? 0 : $item->id;
        $self->oper_type = $type;
        $self->oper_id = $oper_id;
        $self->order_status = $order->status;
        $self->dispatch_status = is_null($item) ? 0 : $item->dispatch_status;
        $self->comment_status = is_null($item) ? 0 : $item->comment_status;
        $self->aftersale_status = is_null($item) ? 0 : $item->aftersale_status;
        $self->refund_status = is_null($item) ? 0 : $item->refund_status;
        $self->remark = $remark;
        $self->save();

        return $self;
    }


    public function oper()
    {
        return $this->morphTo(['oper_type', 'oper_id'], [
            'user' => \sheep\model\user\User::class,
            'admin' => \sheep\model\auth\Admin::class,
            'system' => \sheep\model\auth\Admin::class,
        ]);
    }

}
